package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.*;
import cc.rengu.igas.momp.common.dao.impl.*;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.util.JsonOperation;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 营销活动任务
 * 处理商户/用户信息
 *
 * @author xlz
 * @version 1.0
 * @date 20200428
 */
public class EditMcMerUserTaskService extends RadpService {
    private RgLogger rgLog = RgLog.getLogger(this.getClass().getName());
    private String TASK_STS_INIT = "00";

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {

        /* 查询未处理的任务列表 */
        List<TMMcTaskFlow> taskList = null;
        try {
            taskList = getMerMcTaskList();
            if (taskList != null && taskList.size() > 0) {
                rglog.info("定时任务执行，查询未处理的营销活动商户任务, size：<{}>", taskList.size());
                /* 循环处理任务流水 */
                for (TMMcTaskFlow taskFlow : taskList) {
                    DealMcMchtFileTaskThread taskThread = new DealMcMchtFileTaskThread();
                    taskThread.setTaskFlow(taskFlow);
                    Thread thread = new Thread(taskThread);
                    thread.start();
                }
            }
        } catch (Exception e) {
            rglog.error("营销活动商户任务查询失败, 错误信息：<{}>", e.getMessage());
            e.printStackTrace();
        }

        reset();
        return 0;
    }

    /**
     * 查询任务列表
     *
     * @return
     * @throws Exception
     */
    private List<TMMcTaskFlow> getMerMcTaskList() throws Exception {
        McTaskFlowMapper taskFlowMapper = new McTaskFlowMapperImpl();
        return taskFlowMapper.selectMcTaskFlowList(TASK_STS_INIT);
    }

}
